package com.ysm.smfu.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ysm.smfu.R;
import com.ysm.smfu.adapter.ContactAdapter;
import com.ysm.smfu.bean.Friend;
import com.ysm.smfu.chat.DetailFriendActivity;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.welcome.SharedConfig;
import com.hyphenate.chat.EMClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import chatuidemo.db.DBManager;
import chatuidemo.db.InviteMessgeDao;
import chatuidemo.ui.ChatActivity;
import easeui.EaseConstant;

public class MyXiaJiActivity extends BaseActivity {

    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.myName)
    TextView myName;
    @BindView(R.id.listView)
    ListView listView;

    List<Friend> datas = new ArrayList<>();
    private ContactAdapter adapter;
    private String name;
    private String userId;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_my_xia_ji);
        ButterKnife.bind(this);
        getConfig();
        titleBody.setText("我的好友");
        mainActivityTopbarImg.setVisibility(View.GONE);
    }

    private void getConfig() {
        SharedPreferences sharedPreferences = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        name = sharedPreferences.getString(SpKey.NAME, "");
        SharedPreferences currShared = new SharedConfig(UIUtils.getContext(), name).getPrivateSp();
        userId = currShared.getString(SpKey.USERID, "");
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new ContactAdapter(datas, listView, this);
        listView.setAdapter(adapter);
        adapter.setOnDelMsg(new ContactAdapter.OnDelMsg() {
            @Override
            public void onDelMsg(int position) {
                // 删除联系人
                DBManager.getInstance().deleteContact2(datas.get(position).id);
                EMClient.getInstance().chatManager().deleteConversation(datas.get(position).hxId, true);
                InviteMessgeDao inviteMessgeDao = new InviteMessgeDao(UIUtils.getContext());
                inviteMessgeDao.deleteMessage(datas.get(position).hxId);
//                sendBroadForUpdateMsg();
                delFriend(datas.get(position));
                datas.remove(position);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onOclikItem(int position) {
//                重写条目点击事件
                String hxId = datas.get(position).hxId;
                PrintUtil.printRespones("聊天好友Id:" + hxId);
                UIUtils.startActivity(new Intent(UIUtils.getContext(), ChatActivity.class).putExtra(EaseConstant.EXTRA_USER_ID, hxId));
            }

            @Override
            public void onImageClick(int position) {
                Intent intent = new Intent(MyXiaJiActivity.this, DetailFriendActivity.class);
                intent.putExtra("detailFriend", datas.get(position));
                startActivity(intent);
            }
        });
    }

    /**
     * 删除好友
     * /user/deleteFirend
     * id: 用户id   firendId :好友id
     */
    private void delFriend(final Friend friend) {

        BaseProtocal.builder(true).delFriend(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("删除好友", response);
                if (!TextUtils.isEmpty(response)) {
                    String state = JsonUtil.getState(response);
                    if (TextUtils.equals(state, "1")) {
                        //                从数据库中去除好友
                        DBManager.getInstance().deleteContact2(friend.id);
                        DBManager.getInstance().deleteShenqingContact(friend.id);
                        //删除和某个user会话，如果需要保留聊天记录，传false
                        EMClient.getInstance().chatManager().deleteConversation(friend.hxId, true);
                        InviteMessgeDao inviteMessgeDao = new InviteMessgeDao(UIUtils.getContext());
                        inviteMessgeDao.deleteMessage(friend.hxId);
                    }
                }
            }

            @Override
            public void onFail(Exception e) {

            }
        }, friend.id);
    }
}
